<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <link rel="stylesheet" href="//unpkg.com/element-ui@2.13.2/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../plugins/elementui/index.css">

</head>
<body class="hold-transition">

<div id="app">

    <div class="content-header">
        <h1>系统管理
            <small>菜单管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">

            <div class="filter-container">
                <el-input placeholder="菜单名称" style="width: 200px;" class="filter-item"
                          v-model="pagination.queryString"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建菜单</el-button>
            </div>
            <template>
                <div>
                    <el-table
                            :data="tableData"
                            style="width: 100%;margin-bottom: 20px;"
                            row-key="id"
                            border
                            lazy
                            :load="load"
                            :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                    >
                        <el-table-column
                                prop="name"
                                label="菜单名称"
                                align="center"
                                width="180">
                        </el-table-column>
                        <el-table-column
                                prop="linkUrl"
                                label="访问路径"
                                sortable
                                width="180">
                        </el-table-column>
                        <el-table-column
                                prop="priority"
                                label="路径级别"
                                sortable
                                width="180">
                        </el-table-column>
                        <el-table-column
                                prop="parentMenuId"
                                label="子关联ID"
                                sortable
                                width="180">
                        </el-table-column>
                        <el-table-column
                                prop="level"
                                label="菜单级别"
                                sortable
                                width="180">
                        </el-table-column>
                        <el-table-column
                                label="操作"
                                sortable
                                width="180">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row.id)">修改</el-button>
                                <el-button type="danger" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </template>
            <el-pagination
                    class="pagiantion"
                    @current-change="handleCurrentChange"
                    :current-page="pagination.currentPage"
                    :page-size="pagination.pageSize"
                    layout="total, prev, pager, next, jumper"
                    :total="pagination.total">
            </el-pagination>


            <div class="add-form">
                <el-dialog title="新建菜单" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="user" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="父菜单" prop="parentMenuId" >
                                    <el-select v-model="user.parentMenuId">
                                        <el-option
                                                v-for="item in options"
                                                :key="item.value"
                                                :label="item.label"
                                                :value="item.value">
                                        </el-option>

                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="user.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="链接路径" prop="linkUrl">
                                    <el-input v-model="user.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="优先级" prop="icon">
                                    <el-input type="number" v-model="user.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="图标">
                                    <el-input v-model="user.icon"></el-input>
                                </el-form-item>
                            </el-col>

                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="备注">
                                    <el-input v-model="user.remark" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                     <el-button type="primary" @click="handleAdd()">确定</el-button>

                    </div>
                </el-dialog>
            </div>

            <div class="edit-form">
                <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="user" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="父菜单" prop="parentMenuId">
                                    <el-select v-model="user.parentMenuId">
                                        <el-option
                                                v-for="item in options"
                                                :key="item.value"
                                                :label="item.label"
                                                :value="item.value">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="user.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="链接路径" prop="linkUrl">
                                    <el-input v-model="user.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">

                                <el-form-item label="优先级" prop="priority">
                                    <el-input type="number" v-model="user.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">

                                <el-form-item label="图标" prop="icon">

                                    <el-input v-model="user.icon"></el-input>
                                </el-form-item>
                            </el-col>

                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="备注" prop="description">
                                    <el-input v-model="user.description" type="textarea"></el-input>

                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="addUser()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>

    </div>
</div>


</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@2.13.2/lib/index.js"></script>

<script>
    var app = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            list: [],
            user: {},
            dialogFormVisible: false,
            dialogFormVisible4Edit: false,
            roleList: [],
            roleIds: [],
            username: [],
            options:[],
            tableData:[]

        },
        mounted() {
            this.findPage();
        },
        methods: {
            findparentMenuIds(){
                axios.get("/menu/findparentMenuIds.do").then((res)=>{
                    if (res.data.flag){
                        //数据回显成功
                        this.options=res.data.data
                    }
                })
            },
            handleUpdate(id) {
                //弹出编辑窗口
                this.resetForm();
                this.dialogFormVisible4Edit = true;

                axios.get("/menu/findById.do?id="+id).then((res)=>{
                    if (res.data.flag){
                        this.user=res.data.data
                        this.findparentMenuIds()
                    } else {
                        this.$message.error(res.data.message);
                    }


                })
            },handleDelete(id) {
                //弹出确认框
                this.$confirm('你确定要删除当前数据吗？','提示',{
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送ajax请求，将id提交到Controller
                    axios.get("/menu/delete.do?id="+id).then((res) => {
                        if(res.data.flag){
                            this.$message({
                                type:'success',
                                message:res.data.message
                            });
                            this.findPage();
                        }else{
                            //执行失败
                            this.$message.error(res.data.message);
                        }
                    })
                }).catch(() => {
                    this.$message("已取消");
                });
            },
            resetForm() {
                this.user = {};
            },
            handleCreate() {
                this.resetForm();
                this.dialogFormVisible = true;
                this.findparentMenuIds();
            },
            addUser() {
                this.$refs['dataEditForm'].validate((valid) => {
                    if(valid){
                        //如果校验通过，发送ajax请求，将表单数据提交到Controller
                        axios.post("/menu/update.do",this.user).then((res) => {
                            if(res.data.flag){
                                //处理成功
                                this.$message({
                                    type:'success',
                                    message:res.data.message
                                });
                                //关闭编辑窗口
                                this.dialogFormVisible4Edit = false;
                                //分页查询
                                this.findPage();
                            }else{
                                //处理失败
                                this.$message.error(res.data.message);
                            }
                        });
                    }else{
                        //校验不通过，提示错误信息
                        this.$message.error("表单数据校验失败，请检查输入是否正确！");
                    }
                });
            },
            handleAdd() {
                this.$refs['dataAddForm'].validate((valid) => {
                    if(valid){
                        //如果校验通过，发送ajax请求，将表单数据提交到Controller
                        axios.post("/menu/add.do",this.user).then((res) => {
                            if(res.data.flag){
                                //处理成功
                                this.$message({
                                    type:'success',
                                    message:res.data.message
                                });
                                //关闭新增窗口
                                this.dialogFormVisible = false;
                                //分页查询
                                this.findPage();
                            }else{
                                //处理失败
                                this.$message.error(res.data.message);
                            }
                        });
                    }else{
                        //校验不通过，提示错误信息
                        this.$message.error("表单数据校验失败，请检查输入是否正确！");
                    }
                });
            },
            findPage() {
                var param = {
                    currentPage:this.pagination.currentPage,
                    pageSize:this.pagination.pageSize,
                    queryString:this.pagination.queryString
                };
                //发送ajax请求，进行分页查询
                axios.post("/menu/findPage.do",param).then((res) => {
                    this.row= res.data.rows.id
                    this.pagination.total = res.data.total;
                    this.tableData = res.data.rows;
                });
            },load(tree, treeNode, resolve) {
                console.log(tree)
                setTimeout(() => {
                    resolve(tree.children)
                }, 1000)
            },
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;//指定最新的页码
                this.findPage();
            },
        }
    })
</script>
</html>